package com.utils.failureanalysis.controller;

import com.utils.failureanalysis.data.DataService;
import com.utils.failureanalysis.data.model.singleModule.CountRecorder;
import com.utils.failureanalysis.data.model.singleRecorder.AnalysisData;
import com.utils.failureanalysis.exceptions.fileNotFoundException.PathIsNullException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;

@Controller
@Scope("session")
public class ShowFailsController {
    String userName;
    @Autowired
    DataService dataService;

    @RequestMapping("/login")
    public String index(Model model, String username) {
        this.userName=username;
        model.addAttribute("userName", userName);
        return "index";
    }

    @RequestMapping("/data")
    public String getData(Model model, String testId) {
        try {
            List<AnalysisData> analysisDataList= dataService.getData(testId);
            List<CountRecorder> countRecorderList=dataService.getCountModuleData(testId);
            model.addAttribute("userName", userName);
            model.addAttribute("testId", testId);
            model.addAttribute("analysisDataList", analysisDataList);
            model.addAttribute("countRecorderList", countRecorderList);
        } catch (IOException e) {
            e.printStackTrace();
            model.addAttribute("IOException",e);
        } catch (PathIsNullException e) {
            e.printStackTrace();
            model.addAttribute("PathIsNullException", e);
        }
        return "index";
    }

    @RequestMapping("/detail/{id}")
    public String getDetail(Model model, @PathVariable(name = "id") String id) {
        AnalysisData analysisData=dataService.getDataById(Long.parseLong(id));
        model.addAttribute("analysisData",analysisData);
        return "failureDetails";
    }

    @RequestMapping("/moduleData")
    public String getModuleData(Model model, String testId,String moduleName) {
            List<AnalysisData> analysisDataList= dataService.getDataByTestIdAndModuleName(testId,moduleName);
            model.addAttribute("analysisDataList", analysisDataList);
        return "b";
    }

    @RequestMapping("/allData")
    public String getAllData(Model model) {
        List<AnalysisData> analysisDataList= dataService.getAllData();
        model.addAttribute("analysisDataList", analysisDataList);
        return "b";
    }

    @RequestMapping("/save")
//    public String saveData(Model model, AnalysisData analysisData, BindingResult bindingResult) {
    public String saveData(Model model, AnalysisData analysisData) {
        dataService.save(analysisData);
        List<AnalysisData> analysisDataList= dataService.getAllData();
        model.addAttribute("analysisDataList", analysisDataList);
        return "index";
    }

    @RequestMapping("/saveAll")
    public String saveData(Model model,List<AnalysisData> analysisDataList) {
        dataService.saveAll(analysisDataList);
        analysisDataList= dataService.getAllData();
        model.addAttribute("analysisDataList", analysisDataList);
        return "b";
    }

}
